package com.tc;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.sun.org.apache.xpath.internal.operations.Or;
import com.tc.entity.Card;
import com.tc.entity.Emp;
import com.tc.entity.Order;
import com.tc.entity.User;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Auther: tianchao
 * @Date: 2021/8/28 14:58
 * @Description:
 */
public class TestPoi {

    public List<User> getUsers(){
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            User user = new User();
            user.setId(i+"");
            user.setName("田超_"+i);
            user.setAge(30+i);
            user.setBirthday(new Date());
            user.setStatus(i%2);
            user.setHabbys(Arrays.asList("吃饭","喝酒"));
            // user.setAvatar("C:\\Users\\Administrator\\Desktop\\userface1.jpg");
            user.setAvatar("https://alifei03.cfp.cn/creative/vcg/veer/800water/veer-396859952.jpg");
            Card card = new Card();
            card.setAddress("新民");
            card.setNo("21018119890308833"+i);
            user.setCard(card);
            List<Order> orders = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                Order order = new Order();
                order.setNo("1000"+i+""+j);
                order.setDesc("苹果"+(i+j)+"个");
                orders.add(order);
            }
            user.setOrders(orders);
            list.add(user);
        }
        return list;
    }

    @Test
    public void testExport() throws IOException {
        List<User> users = getUsers();
        ExportParams exportParams = new ExportParams();
        exportParams.setSheetName("用户信息");
        exportParams.setTitle("用户列表");
        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, User.class, users);
        sheets.write(new FileOutputStream("excel/test.xls"));
    }

    @Test
    public void testImport(){
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(1);
        importParams.setHeadRows(2);
        List<Emp> objects = ExcelImportUtil.importExcel(new File("excel/import.xls"), Emp.class, importParams);
        objects.stream().forEach(System.out::println);
    }
}
